<include file="Public:header" />
<div class="alert alert-info">
    菜式添加
</div>
<form class="form-horizontal" method="post" action="__URL__/add" onsubmit="return ajax_submit(this);">
    <div class="control-group">
        <label class="control-label">菜品名称<span class="require">*</span></label>
        <div class="controls">
            <input type="text" name="food_name" placeholder="长度不超过30位" maxlength="30" required>
        </div>
    </div>
    <div class="control-group">
        <label class="control-label">菜品类型<span class="require">*</span></label>
        <div class="controls">
            <select name="food_type">
                <option value="-1">-- 请选择一个菜式类型 --</option>
                <option value="本单位">本单位</option>
                <option value="外来单位">外来单位</option>
            </select>
        </div>
    </div>
    <div class="control-group">
        <label class="control-label">菜品主料<span class="require">*</span></label>
        <div class="controls">
            <div class="copy">
                <a href="" class="btn add-main" onclick="return addItem(this)"><i class="icon-plus"></i></a>
                <select name="main_good[]" class="main-good" onchange="getUnitAndPrice(this);">
                    <option value="-1">-- 请选择一个主料 --</option>
                    <volist name="mainGood" id="val">
                        <option value="{$val.id}">{$val.stuff_name}</option>
                    </volist>
                </select>
                &nbsp;数量：<input type="text" name="main_num[]" class="num main-num" value="1" onkeyup="changeByNum(this);"/>
                &nbsp;单位：<input type="text" class="unit main-unit" name="main_unit[]" />
                &nbsp;单价：<input type="text" class="unit main-price" name="main_price[]" onkeyup="changeByPrice(this);"/> <input type="hidden" class="main-price-temp"/>
                &nbsp;总价：<input type="text" class="unit main-row-count row-count" disabled/>
        </div>
    </div>
    <div class="control-group">
        <label class="control-label">菜品辅料<span class="require">*</span></label>
        <div class="controls">
            <div class="copy">
                <a href="" class="btn add-sur" onclick="return addItem(this);"><i class="icon-plus"></i></a>
                <select name="sur_good[]" class="sur-good" onchange="getUnitAndPrice(this);">
                    <option value="-1">-- 请选择一个辅料 --</option>
                    <volist name="surGood" id="val">
                        <option value="{$val.id}">{$val.stuff_name}</option>
                    </volist>
                </select>
                &nbsp;数量：<input type="text" name="sur_num[]" class="num sur-num" value="1" onkeyup="changeByNum(this);"/>
                &nbsp;单位：<input type="text" class="unit sur-unit" name="sur_unit[]" />
                &nbsp;单价：<input type="text" class="unit sur-price" name="sur_price[]" onkeyup="changeByPrice(this);"/>
                &nbsp;总价：<input type="text" class="unit sur-row-count row-count" disabled/>
            </div>
        </div>
    </div>
    <div class="control-group">
        <label class="control-label">菜式总价<span class="require">*</span></label>
        <div class="controls">
            <input type="text" class="count" disabled/>
            <input type="hidden" class="count" name="food_price_count"/>
        </div>
    </div>
    <div class="control-group">
        <div class="controls">
            <input type="submit" value="确　定" class="btn btn-primary" data-target="#myModal"/>
            <span class="btn btn-warning" onclick="history.back(-1);">返　回</span>
        </div>
    </div>
    <input type="hidden" name="send" value="sub"/>
</form>

<include file="Public:check"/>

<script type="text/javascript">
    function getUnitAndPrice(_this)
    {
        var id = $(_this).val();
        var temp = $(_this).attr('class').split('-');
        var selectClass = temp[0];
        var index = $('select.'+selectClass+'-good').index($(_this));
        if(id != -1)
        {
            $.ajax({
                'url': '__URL__/getUnitAndPrice',
                'type': 'post',
                'async': false,
                'dataType': 'json',
                'data': {
                    'id': id
                },
                'success': function (result) {
                    $('input.' + selectClass + '-unit').eq(index).val(result.stuff_unit);
                    $('input.' + selectClass + '-price').eq(index).val(result.stuff_price);
                    $('input.' + selectClass + '-price-temp').eq(index).val(result.stuff_price);
                }
            });

            var price = $('input.' + selectClass + '-price').eq(index).val();
            var num = $('input.' + selectClass + '-num').eq(index).val();
            $('.' + selectClass + '-row-count').eq(index).val((num * price).toFixed(2));
        }
        else
        {
            $('input.' + selectClass + '-unit').eq(index).val('');
            $('input.' + selectClass + '-price').eq(index).val('');
            $('input.' + selectClass + '-price-temp').eq(index).val('');
            $('.' + selectClass + '-row-count').eq(index).val('');
        }

        var count = 0;

        $.each($('input.row-count'), function() {
            var val = Number($(this).val());
            if(val != '')
                count = count+val;
        });
        $('input.count').val(count.toFixed(2));

    }

    function addItem(_this)
    {
        var controls = $(_this).parent().parent();
        var data = '<div class="copy">'+controls.find('.copy').html()+'</div>';
        $(controls).append(data);

        return false;
    }

    function changeByNum(_this)
    {
        if(isNaN($(_this).val()))
            $(_this).val(1);
        var temp = $(_this).prev().attr('class').split('-');
        var selectClass = temp[0];
        var index = $('.'+selectClass+'-num').index($(_this));

        var price = $('input.'+selectClass+'-price').eq(index).val();
        $('.'+selectClass+'-row-count').eq(index).val(($(_this).val() * price).toFixed(2));

        var count = 0;

        $.each($('input.row-count'), function() {
            var val = Number($(this).val());
            if(val != '')
                count = count+val;
        });
        $('input.count').val(count.toFixed(2));
    }

    function changeByPrice(_this)
    {
        var temp = $(_this).prev().prev().prev().attr('class').split('-');
        var selectClass = temp[0];
        var index = $('.'+selectClass+'-price').index($(_this));

        if(isNaN($(_this).val()))
            $(_this).val($('input.'+selectClass+'-price-temp').eq(index).val());


        var num = $('input.'+selectClass+'-num').eq(index).val();
        $('.'+selectClass+'-row-count').eq(index).val(($(_this).val() * num).toFixed(2));

        var count = 0;

        $.each($('input.row-count'), function() {
            var val = Number($(this).val());
            if(val != '')
                count = count+val;
        });
        $('input.count').val(count.toFixed(2));
    }

</script>

</body>
</html>